using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;

namespace Index_Management
{
    public partial class Form2 : Form
    {
        public Server DataSource;
        public Database DB;
        public Boolean Conn;
        private string ConnectionFailMsg;
        public Form2()
        {
            InitializeComponent();
        }

        public void InitializeObjects()
        {
            DataSource = new Server();
            DB = new Database();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            InitializeObjects();
        }
      
        private void SetConnection()
        {
            try
            {
                if (radioButton_WinAuth.Checked == true)
                {
                    comboBox_Catalog.Items.Clear();
                    DataSource = new Server(textBox_DataSource.Text);
                    foreach (Database DB in DataSource.Databases)
                    {
                        comboBox_Catalog.Items.Add(DB.Name);
                    }
                }
                else
                {
                    comboBox_Catalog.Items.Clear();
                    DataSource = new Server(textBox_DataSource.Text);
                    DataSource.ConnectionContext.LoginSecure = false;
                    DataSource.ConnectionContext.Login = textBox_SQLUser.Text;
                    DataSource.ConnectionContext.Password = textBox_SQLPassword.Text;
                    foreach (Database DB in DataSource.Databases)
                    {
                        comboBox_Catalog.Items.Add(DB.Name);
                    }
                }
            }
            catch (Exception E)
            {
                MessageBox.Show("Could not connect to the server " + textBox_DataSource.Text + ". " + E.Message);
                comboBox_Catalog.Items.Clear();
            }
        }

        private Boolean TestConnection()
        {
            if (comboBox_Catalog.Text == "")
            {
                ConnectionFailMsg = "Please select a database first.";
                return false;
            }
            try
            {
                if (radioButton_WinAuth.Checked == true)
                {
                    DataSource = new Server(textBox_DataSource.Text);
                    DB = DataSource.Databases[comboBox_Catalog.SelectedIndex];
                }
                else
                {
                    DataSource = new Server(textBox_DataSource.Text);
                    DataSource.ConnectionContext.LoginSecure = false;
                    DataSource.ConnectionContext.Login = textBox_SQLUser.Text;
                    DataSource.ConnectionContext.Password = textBox_SQLPassword.Text;
                    DB = DataSource.Databases[comboBox_Catalog.SelectedIndex];
                }
                //MessageBox.Show("Connection succeeded.");
                return true;
            }
            catch (Exception E)
            {
                //MessageBox.Show("Connection failed.");
                ConnectionFailMsg = E.Message;
                return false;
            }
        }

        private void button_TestConn_Click(object sender, EventArgs e)
        {
            if (TestConnection() == true)
            {
                MessageBox.Show("Connection succeeded.");
            }
            else
            {
                MessageBox.Show("Connection failed. " + ConnectionFailMsg);
            }
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            if (TestConnection() == true)
            {
                Conn = true;
                this.Hide();
            }
            else
            {
                MessageBox.Show("Connection failed. " + ConnectionFailMsg);
                Conn = false;
            }
        }

        public Server getDataSource()
        {
            return DataSource;
        }

        public Database getDB()
        {
            return DB;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton_SQLAuth.Checked == true)
            {
                label_SQLUser.Enabled = true;
                label_SQLPassword.Enabled = true;
                textBox_SQLUser.Enabled = true;
                textBox_SQLPassword.Enabled = true;
                comboBox_Catalog.Items.Clear();
            }
            else
            {
                label_SQLUser.Enabled = false;
                label_SQLPassword.Enabled = false;
                textBox_SQLUser.Enabled = false;
                textBox_SQLPassword.Enabled = false;
                comboBox_Catalog.Items.Clear();
            }

        }

        public Boolean getConnectionStatus()
        {
            return Conn;
        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            Conn = false;
            this.Dispose();
        }

        private void textBox_DataSource_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox_Catalog_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetConnection();
        }

    }
}